<template>
  <el-cascader
    v-if="tier"
    :options="tree"
    :value="value"
    :props="{
      value:'id',
      label: 'label',
      children: 'children',
      emitPath: false,
      checkStrictly: true,
    }"
    clearable
    filterable
    :show-all-levels="false"
    :disabled="disabled"
    @input="change($event)"
  />

  <el-select
    v-else
    :value="value"
    filterable
    :remote="remote"
    :remote-method="remoteMethod"
    :loading="loading"
    :multiple="multiple"
    :size="isMini"
    :placeholder="placeholder"
    @input="change($event)"
  >
    <el-option
      v-for="(item, index) in list"
      :key="index"
      :label="item.name"
      :value="item.id"
      :disabled="disabled"
    />
    <!--    <li class="select-page">-->
    <!--      <el-pagination-->
    <!--        :total="total"-->
    <!--        :current-page.sync="page"-->
    <!--        :page-size="size"-->
    <!--        @current-change="index"-->
    <!--        layout="prev, pager, next"-->
    <!--        :hide-on-single-page="true"-->
    <!--        :page-count="count"-->
    <!--        :pager-count="5"-->
    <!--      ></el-pagination>-->
    <!--    </li>-->
  </el-select>
</template>

<script>
import API from '../api/common/select'
import { arr2Tree } from '@/utils/help'
export default {
  props: {
    multiple: {
      type: Boolean,
      default: false
    },
    value: {
      type: [Number, Array],
      default: null
    },
    action: {
      type: String,
      default: 'adminList'
    },
    query: {
      type: Object,
      default() {
        return {}
      }
    },
    disabled: {
      type: Boolean,
      default: false
    },
    tier: {
      type: Boolean,
      default: false
    },
    isTop: {
      type: Boolean,
      default: false
    },
    top: {
      type: Object,
      default() {
        return {
          id: 0,
          name: '顶级分类'
        }
      }
    },
    isMini: {
      type: String,
      default: 'mini'
    },
    placeholder: {
      type: String,
      default: '请选择'
    }
  },
  data() {
    return {
      list: [],
      is_page: false,
      remote: false,
      loading: false,
      page: 1,
      size: 15,
      first: 1
    }
  },
  computed: {
    count() {
      // return Math.ceil(this.total / this.size);
      return 1
    },
    tree() {
      return arr2Tree(this.list)
    }
  },
  watch: {
    query: {
      deep: true,
      handler(value) {
        this.index(value)
      }
    },
    action() {
      this.index()
    }
  },
  created() {
    // this.disabled
    this.index({ id: this.value })
  },
  methods: {
    index(query) {
      this.loading = true
      API.option({
        query: { ...query, ...this.query },
        action: this.action,
        page: this.page
      }).then((response) => {
        if (response.data.data) {
          this.loading = false
          this.list = response.data.data
          if (this.isTop) {
            this.list.unshift(this.top)
          }

          const thisSon = []
          for (let i = 0; i < response.data.data.length; i++) {
            thisSon.push(response.data.data[i].id)
          }
          if (thisSon.indexOf(this.value) < 0) {
            this.$emit('input', null)
          }
        } else {
          this.loading = false
          this.list = response.data
          const thisSon = []
          for (let i = 0; i < response.data.length; i++) {
            thisSon.push(response.data[i].id)
          }
          if (thisSon.indexOf(this.value) < 0) {
            this.$emit('input', null)
          }
        }
        // if (response.data.total !== undefined) {
        //   this.is_page = true;
        //   this.remote = true;
        //   this.total = response.data.total;
        // }

        // this.tier || this.list.unshift({ id: 0, label: "请选择" });
      })
    },
    remoteMethod(label) {
      this.page = 1
      this.index({ label: label })
    },
    change: function(val) {
      this.$emit('input', val)
    }
  }
}
</script>
